home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / TUTORIAL.BIN / BinaryField.class (.txt) < prev    next >
Encoding:
Java Class File  |  1997-01-30  |  2.6 KB  |  81 lines

  1. package symantec.itools.db.net;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.DataOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import symjava.sql.SQLException;
  8.  
  9. abstract class BinaryField extends Field {
  10.    byte[] _binData;
  11.  
  12.    void read(DataInputStream is) throws SQLException, IOException, ErrorException {
  13.       super.read(is);
  14.       this._binData = new byte[0];
  15.    }
  16.  
  17.    void write(DataOutputStream os) throws IOException {
  18.       super.write(os);
  19.    }
  20.  
  21.    public String getString() throws SQLException {
  22.       return ((Field)this).isNull() ? null : new String(this._binData, 0, 0, this._binData.length);
  23.    }
  24.  
  25.    public byte[] getBytes() throws SQLException {
  26.       return ((Field)this).isNull() ? new byte[0] : this._binData;
  27.    }
  28.  
  29.    public InputStream getAsciiStream() throws SQLException {
  30.       return new BinaryInputStream(this._binData);
  31.    }
  32.  
  33.    public InputStream getUnicodeStream() throws SQLException {
  34.       return new BinaryInputStream(this._binData);
  35.    }
  36.  
  37.    public InputStream getBinaryStream() throws SQLException {
  38.       return new BinaryInputStream(this._binData);
  39.    }
  40.  
  41.    public void setString(String x) throws SQLException {
  42.       this._binData = new byte[x.length()];
  43.       x.getBytes(0, x.length() - 1, this._binData, 0);
  44.       super._null = false;
  45.    }
  46.  
  47.    public void setBytes(byte[] x) throws SQLException {
  48.       this._binData = x;
  49.       super._null = false;
  50.    }
  51.  
  52.    public void setStream(InputStream x, int length) throws SQLException {
  53.       if (length >= 0 && length <= 255) {
  54.          byte[] b = new byte[length];
  55.  
  56.          try {
  57.             x.read(b, 0, length);
  58.          } catch (IOException e) {
  59.             throw new SQLException(((Throwable)e).getMessage());
  60.          }
  61.  
  62.          this._binData = b;
  63.          super._null = false;
  64.       } else {
  65.          throw new SQLException("Stream length out of range.");
  66.       }
  67.    }
  68.  
  69.    public void setAsciiStream(InputStream x, int length) throws SQLException {
  70.       this.setStream(x, length);
  71.    }
  72.  
  73.    public void setUnicodeStream(InputStream x, int length) throws SQLException {
  74.       this.setStream(x, length);
  75.    }
  76.  
  77.    public void setBinaryStream(InputStream x, int length) throws SQLException {
  78.       this.setStream(x, length);
  79.    }
  80. }
  81.